// Copyright (c) 2008 Matthew Johnson
// Licensed under the MIT License (see license.txt)
package actionml.interaction {
	import actionml.Global;
	
	import flash.display.DisplayObject;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.ui.Keyboard;

	public class TextInteractions {
		public static function initialize() {
			Global.initializeExtension(TextInteractions, DisplayObject);
		}
		public static var instanceMethods:Object = {
			maskInput: function(options:Object = null) {
				var inputMask:RegExp = new RegExp(options["inputMask"]);
				this.addEventListener("textInput", function(e:TextEvent) {
					if (!(inputMask.test(e.text))) {
						e.preventDefault();
					}
				});				
			},
			makeScrollableText: function(options:Object = null) {
				if (this.isAlready("ScrollableText")) return;
				var text:Object = this.getValueForKeypath(options["text"] || "text");
				var scrollbar:Object = this.getValueForKeypath(options["scrollbar"] || "scrollbar");
				var hScrollbar:Object = this.getValueForKeypath(options["hScrollbar"] || "hScrollbar");
				options.minValue = 1;
				options.stepSize = 1;
				if (text.text.match(/^\s*$/)) {
					text.text = "";	
				}
				if (options["text"]) {
					text.text = options["text"];
				}
				if (options["htmlText"]) {
					text.htmlText = options["htmlText"];
				}
				scrollbar.makeSlider(options);
				scrollbar.addEventListener("change", function() {
					text.scrollV = Math.round(scrollbar.getSliderValue());
				});
				if (hScrollbar) {
					options.minValue = 0;
					hScrollbar.makeSlider(options);
					hScrollbar.addEventListener("change", function() {
						text.scrollH = Math.round(hScrollbar.getSliderValue());
					});
				}
				var configureScrollbars:Function = function() {
					if (hScrollbar) {
						hScrollbar.setMaxValue(text.maxScrollH);
						hScrollbar.setThumbPercent(text.width / text.textWidth); // not sure if this is right
						hScrollbar.visible = (text.maxScrollH > 1 && !text.wordWrap);
					} else {
						scrollbar.setMaxValue(text.maxScrollV);
						var visible:Number = text.bottomScrollV - text.scrollV + 1;
						scrollbar.setThumbPercent(visible / (text.maxScrollV + visible - 1));
						scrollbar.visible = (text.maxScrollV > 1);
					}
				};
				configureScrollbars();
				this.addEventListener(MouseEvent.MOUSE_WHEEL, function(e:MouseEvent) {
					// TODO: try to get mouse wheel working
					trace("Please tell Matthew what you did if you see this.\n" + this + ": " + this.name + "\n" + e);
				});
				text.addEventListener("change", function() {
					configureScrollbars();
				});
				this.getText = function() {
					return text.text;
				};
				this.setText = function(t:String) {
					text.text = t || "";
					configureScrollbars();
				};
				this.getHtmlText = function() {
					return text.htmlText;
				};
				this.setHtmlText = function(t:String) {
					text.htmlText = t || "";
					configureScrollbars();
				};
			},
			makeTextAreaAccessible: function() {
				this.focusManager.deactivate();
				this.tabChildren = false;	
				var tf:TextField = this.textField;
				var pageSize:int = tf.bottomScrollV + 1;
				tf.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent) {
					if (e.keyCode == Keyboard.DOWN && tf.scrollV < tf.maxScrollV) {
						tf.scrollV++;
					}
					if (e.keyCode == Keyboard.UP && tf.scrollV > 1) {
						tf.scrollV--;
					}
				});
				tf.addEventListener(FocusEvent.FOCUS_IN, function(e:FocusEvent) {
					var i:int = tf.getCharIndexAtPoint(1, tf.height - 2);
					var lh:Number = tf.getLineMetrics(tf.getLineIndexOfChar(i)).height;
					i = tf.getCharIndexAtPoint(1, tf.height - 2 - lh);
					tf.setSelection(i, i);
				});
			}
			/*
			pageableText: function(options:* = null) {
				if (options && "previous" in options && options.previous != null && options.previous != undefined) {
					this.previousButton = this[options.previous];
				} else {
					this.previousButton = this.getChildByName("previous");
					if (this.previousButton == null) {
						this.previousButton = this.getChildByName("previousButton");
					}
				}
				this.previousButton.makeButton();
				if (options && "next" in options && options.next != null && options.next != undefined) {
					this.nextButton = this[options.next];
				} else {
					this.nextButton = this.getChildByName("next");
					if (this.nextButton == null) {
						this.nextButton = this.getChildByName("nextButton");
					}
				}
				this.nextButton.makeButton();
				this.page = 1;
				var o:DisplayObject = this;
				var disabledAlpha:Number = o.disabledAlpha || .4;
				loadPage(1);				
				this.nextButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
					o.page +=1;
					loadPage(o.page);
				});
				this.previousButton.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
					o.page -=1;					
					loadPage(o.page);
				});
				function loadPage(page:int) {
					o.leftPage.htmlText = o["leftPage" + page];
					o.rightPage.htmlText = o["rightPage" + page];
					if (!(("leftPage" + (page + 1)) in o)) {
						o.nextButton.alpha = disabledAlpha;
						o.nextButton.persistentAlpha = disabledAlpha;
						o.nextButton.mouseEnabled = false;
						o.nextButton.tabEnabled = false;
						if (o.stage) {
							o.stage.focus = o;
						}
					} else {
						o.nextButton.alpha = 1;
						o.nextButton.persistentAlpha = 1;
						o.nextButton.mouseEnabled = true;
						o.nextButton.tabEnabled = true;
					}
					if (!(("rightPage" + (page - 1)) in o)) {
						o.previousButton.alpha = disabledAlpha;
						o.previousButton.persistentAlpha = disabledAlpha;
						o.previousButton.mouseEnabled = false;						
						o.previousButton.tabEnabled = false;
						if (o.stage) {	
							o.stage.focus = o;
						}
					} else {
						o.previousButton.alpha = 1;
						o.previousButton.persistentAlpha = 1;
						o.previousButton.mouseEnabled = true;
						o.previousButton.tabEnabled = true;
					}
				}
			}*/
		};
	}
}